﻿using System;
using System.Collections.Generic;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0101", Name = "期间项目逻辑违反", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0101:VerfityOneResult
    {
        
        /// <summary>
        /// 默认的检查方式倒序
        /// </summary>
        private EnumDateSort _dataSort = EnumDateSort.Asc;

        /// <summary>
        /// 日期结果顺序
        /// 正序 倒序
        /// </summary>
        public EnumDateSort DateSort
        {
            get { return _dataSort; }
            set { _dataSort = value; } 
        }



        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            foreach (List<string> rowResult in result)
            {
                int emptyCount = EmptyColumnCount(rowResult);

                //期间项目有缺失
                if (emptyCount >0 )
                {
                    continue;
                }

                ////结果中有？ 不比较
                int questionCount = QuestionMarkColumnCount(rowResult);

                if (questionCount > 0)
                {
                    continue;
                }
                
                if (CheckSameResult(rowResult))
                {
                    continue;
                }

                List<string> changeResult = new Split.DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(rowResult);

                //add 20130927
                changeResult= base.CompleteDate(changeResult);

                //找到录入结果中的 日期
                changeResult = changeResult.Select(t=> new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat,t).GetDate() ).ToList();

                List<string> sortRowResult = null;
                if (this.DateSort == EnumDateSort.Asc)
                {
                    sortRowResult = changeResult.OrderBy(t => t, new DateEntryResultComparer()).ToList();
                }
                else if (this.DateSort == EnumDateSort.Desc)
                {
                    sortRowResult = changeResult.OrderByDescending(t => t, new DateEntryResultComparer()).ToList();
                }

                bresult = sortRowResult != null && changeResult.SequenceEqual(sortRowResult);

                if (bresult)
                {
                    System.Console.WriteLine("VerfityRq0101");
                    break;
                }
            }

            return bresult;   
        }

        private bool CheckSameResult(List<string> result)
        {
            bool bResult = true;

            if (result == null || result.Count==0)
                return bResult;

            List<string> AscList = result.OrderBy(t => t, new DateEntryResultComparer()).ToList();
            List<string> DescList = result.OrderByDescending(t => t, new DateEntryResultComparer()).ToList();

            bResult = result != null && AscList.SequenceEqual(DescList);

            return bResult;
        }
    }
}